<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" >
  <xsl:output method="xml" version="1.0" omit-xml-declaration="yes" indent="yes" encoding="utf-8"/>

  <xsl:variable name="UnitCount" select="count(/ProjectDataSet/Project/BacklogItemUnit[Type != 6])" />
  <xsl:variable name="SprintCount" select="count(/ProjectDataSet/Project/Sprint)" />

  <xsl:include href="include/Slices.xslt"/>
  
  <xsl:variable name="currentBaseline" select="/ProjectDataSet/Project/Baseline" />


  <xsl:variable name="previuosSprintNumber">
    <xsl:choose>
      <xsl:when test="$currentBaseline &lt;= 1 ">
        <xsl:value-of select="1"/>
      </xsl:when>
      <xsl:when test="$currentBaseline &gt;= 2">
        <xsl:value-of select="$currentBaseline - 1"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="/ProjectDataSet/Project/Sprint[SprintUId=/ProjectDataSet/Project/LastSprintUId]/SprintNumber"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:variable>



  <xsl:template match="/">
    <FlowDocument
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/xps/2005/06/resourcedictionary-key"
      xmlns:BadHabit_Factory_Controls="clr-namespace:BadHabit.Factory.Controls;assembly=Factory"
      
      PageWidth="21cm"
      PagePadding="60,40,40,40"
      PageHeight="29.7cm"
      ColumnWidth="21 cm">


      <xsl:call-template name="StyleSettings"/>

      <xsl:call-template name="SprintHeader">
        <xsl:with-param name="type" select="'Review'"/>
        <xsl:with-param name="sprintNumber" select="$previuosSprintNumber"/>
      </xsl:call-template>

      <xsl:call-template name="BaseLineCheck"/>

      <Table Style="{{StaticResource NoBorderTable}}" Margin="0,15,0,0">
        <TableRowGroup>
          <TableRow>
            <TableCell Style="{{StaticResource NoBorderCell}}" Padding="0,0,5,0">
              <BlockUIContainer>
                <Border CornerRadius="5,5,5,5"  BorderBrush="#AAAAAA" BorderThickness="1" Padding="5">
                  <TextBlock FontFamily="Arial" FontSize="15">
                    Deadline
                    <xsl:value-of select="/ProjectDataSet/Project/EstimatedEndDateString"/>
                  </TextBlock>
                </Border>
              </BlockUIContainer>
            </TableCell>            
            <TableCell Style="{{StaticResource NoBorderCell}}" Padding="5,0,5,0">
              <BlockUIContainer>
                <Border CornerRadius="5,5,5,5"  BorderBrush="#AAAAAA" BorderThickness="1" Padding="5" >
                  <TextBlock FontFamily="Arial" FontSize="15">
                    <xsl:value-of select="format-number(/ProjectDataSet/Project/WalkedIndicator,'0,0','pt-br-Decimal')"/>
                    % walked
                  </TextBlock>
                </Border>
              </BlockUIContainer>
            </TableCell>              
            <TableCell Style="{{StaticResource NoBorderCell}}"  Padding="5,0,0,0">
              <BlockUIContainer>
                <xsl:variable name="borderBrush">
                  <xsl:choose>                    
                    <xsl:when test="/ProjectDataSet/Project/DeadlineIndicator &lt; 0">Red</xsl:when>
                    <xsl:otherwise>#AAAAAA</xsl:otherwise>
                  </xsl:choose>
                </xsl:variable>
                <xsl:variable name="borderThickness">
                  <xsl:choose>
                    <xsl:when test="/ProjectDataSet/Project/DeadlineIndicator &lt; 0">3</xsl:when>
                    <xsl:otherwise>1</xsl:otherwise>
                  </xsl:choose>
                </xsl:variable>
                <Border CornerRadius="5,5,5,5"  BorderBrush="{$borderBrush}" BorderThickness="{$borderThickness}" Padding="5">
                  <TextBlock FontFamily="Arial" FontSize="15">                    
                    <xsl:choose>
                      <xsl:when test="/ProjectDataSet/Project/DeadlineIndicator &gt; 0">
                        <xsl:value-of select="format-number(/ProjectDataSet/Project/DeadlineIndicator,'0,0','pt-br-Decimal')"/> hours a head
                      </xsl:when>
                      <xsl:when test="/ProjectDataSet/Project/DeadlineIndicator &lt; 0">
                        <xsl:value-of select="format-number(/ProjectDataSet/Project/DeadlineIndicator * -1,'0,0','pt-br-Decimal')"/> hours late
                      </xsl:when>
                      <xsl:otherwise>
                        No project delays
                      </xsl:otherwise>
                    </xsl:choose>
                  </TextBlock>
                </Border>
              </BlockUIContainer>
            </TableCell>
          </TableRow>
        </TableRowGroup>
      </Table>




      <BlockUIContainer>


        <BadHabit_Factory_Controls:BurnDownGraph
                                 
                                 FontFamily="Arial"
                                 Margin="20,30,20,10"
                                 Height="350"                                 
                                 AllowNavigation="False"
                                 HorizontalAlignment="Stretch"
                                 />

      </BlockUIContainer>

      <Paragraph Style="{{StaticResource TitleParagraph}}">
        Risks
      </Paragraph>

   
      
        <Table Style="{{StaticResource NoBorderTable}}">

          <Table.Columns>
            <TableColumn />
            <TableColumn />
            <TableColumn Width="80" />
            <TableColumn Width="80" />
          </Table.Columns>

          <TableRowGroup>
            <TableRow>
              <TableCell Style="{{StaticResource DoubleLineCell}}">
                <Paragraph>
                  <Bold>Risk</Bold>
                </Paragraph>
              </TableCell>
              <TableCell Style="{{StaticResource DoubleLineCell}}">
                <Paragraph>
                  <Bold>Actions</Bold>
                </Paragraph>
              </TableCell>
              <TableCell Style="{{StaticResource DoubleLineCell}}">
                <Paragraph TextAlignment="Center">
                  <Bold>Impact</Bold>
                </Paragraph>
              </TableCell>
              <TableCell Style="{{StaticResource DoubleLineCell}}">
                <Paragraph TextAlignment="Center">
                  <Bold>Probability</Bold>
                </Paragraph>
              </TableCell>
            </TableRow>

            <xsl:for-each select="/ProjectDataSet/Project/Risk[Probability &gt; 0]">
              <xsl:sort select="Probability" data-type="number" order="descending"/>
              <xsl:sort select="Impact" data-type="number"  order="descending"/>              
              <TableRow>
                <TableCell Style="{{StaticResource SingleLineCell}}">
                  <Paragraph>
                    <xsl:value-of select="RiskName" />
                  </Paragraph>
                </TableCell>
                <TableCell Style="{{StaticResource SingleLineCell}}">
                  <Paragraph>
                    <xsl:value-of select="Actions" />
                  </Paragraph>
                </TableCell>
                <TableCell Style="{{StaticResource SingleLineCell}}">
                  <Paragraph TextAlignment="Center">
                    <xsl:call-template name="ImpactSignal">
                      <xsl:with-param name="impact" select="Impact"/>
                    </xsl:call-template>
                  </Paragraph>
                </TableCell>
                <TableCell Style="{{StaticResource SingleLineCell}}">
                  <Paragraph TextAlignment="Center">
                    <xsl:call-template name="ImpactSignal">
                      <xsl:with-param name="impact" select="Probability"/>
                    </xsl:call-template>
                  </Paragraph>
                </TableCell>
              </TableRow>
            </xsl:for-each>

          </TableRowGroup>
        </Table>

      <xsl:if test="count(/ProjectDataSet/Project/Risk[Probability &gt; 0]) = 0">
        <Paragraph>
          The project does not have any risk.
        </Paragraph>
      </xsl:if>

      <Paragraph Style="{{StaticResource TitleParagraph}}">
        <Bold>
          Items delivired at this Sprint (<xsl:value-of select="$previuosSprintNumber" />&#186; Sprint)          
        </Bold>
      </Paragraph>

      <xsl:call-template name="DeliveryItems">
        <xsl:with-param name="sprintUId" select="/ProjectDataSet/Project/Sprint[SprintNumber=$previuosSprintNumber]/SprintUId"/>
        <xsl:with-param name="plannedItems" select="/ProjectDataSet/Project/BacklogItem[DeliverySprintNumber=$previuosSprintNumber and Status=2]" />
      </xsl:call-template>



  
      <xsl:variable name="nextDeliverySprintUId">
        <xsl:value-of select="/ProjectDataSet/Project/Sprint[SprintNumber=$currentBaseline]/SprintUId"/>
      </xsl:variable>
      <xsl:variable name="nextDeliverySprintNumber" select="$currentBaseline" />

      <xsl:if test="$nextDeliverySprintNumber &gt; $previuosSprintNumber">
        
      
      
        <Paragraph Style="{{StaticResource TitleParagraph}}">
          <Bold>
            Planned deliveries for <xsl:value-of select="$nextDeliverySprintNumber" />&#186; Sprint, at <xsl:value-of select="/ProjectDataSet/Project/Sprint[SprintUId=$nextDeliverySprintUId]/ExpirationDateString" />
          </Bold>
        </Paragraph>
      <Paragraph>Sorted from the top priority to the less one.</Paragraph>

      <xsl:call-template name="DeliveryItems">
        <xsl:with-param name="sprintUId" select="$nextDeliverySprintUId"/>
        <xsl:with-param name="plannedItems" select="/ProjectDataSet/Project/BacklogItem[DeliverySprintNumber=$nextDeliverySprintNumber]" />
      </xsl:call-template>


      </xsl:if>
    

    </FlowDocument>
  </xsl:template>


</xsl:stylesheet>